package com.ebelter.myehc.ui.fragments;


import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.products.scale.ScaleManager;
import com.ebelter.btcomlib.models.bluetooth.products.scale.bean.ScaleMeasureResult;
import com.ebelter.btcomlib.models.bluetooth.products.scale.bean.ScaleMeasureResultAnalyzer;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.LevelColorView;
import com.ebelter.myehc.R;
import com.ebelter.myehc.ui.view.CombinView5;
import com.ebelter.myehc.utils.EHCCommonUtils;
import com.ebelter.myehc.utils.ViewUtilsEHC;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;


/**
 * 体脂秤
 */

public class Scale_Fragment extends BaseFragment {
    public static final String TAG = "Scale_Fragment";

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_scale;
    }

    private TextView activityTopTittleTv;

    private LevelColorView levelColorView;

    private CombinView5 cb5_s_weight;
    private CombinView5 cb5_s_fat;
    private CombinView5 cb5_s_muscle;
    private CombinView5 cb5_s_bmr;
    private CombinView5 cb5_s_fatlevel;
    private CombinView5 cb5_s_water;
    private CombinView5 cb5_s_guyanliang;
    private CombinView5 cb5_s_protein;
    private CombinView5 cb5_s_bmi;

    public void setActivityTopTittleTv(TextView activityTopTittleTv) {
        this.activityTopTittleTv = activityTopTittleTv;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        adapterUnit();
        initDisplayValue();
    }

    private void initDisplayValue() {
        setCb5Title2s(N3,N3,N3,N,N3,N3,N3,N3,N3);
    }

    //先匹配单位
    private void adapterUnit() {
        if (ScaleUser.getUser().isKgModel()){
         setCb5Title3(ScaleMeasureResultAnalyzer.kg,cb5_s_weight,cb5_s_muscle,cb5_s_guyanliang);
        }else {
            setCb5Title3(ScaleMeasureResultAnalyzer.lb,cb5_s_weight,cb5_s_muscle,cb5_s_guyanliang);
        }

        if (AppUtils.en.equals(AppUtils.getLocalStr())){
            ViewUtilsEHC.setCb5Title1Size(cb5_s_weight,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_fat,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_muscle,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_bmr,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_fatlevel,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_water,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_guyanliang,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_protein,12);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_bmi,12);
        }else {
            ViewUtilsEHC.setCb5Title1Size(cb5_s_weight,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_fat,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_muscle,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_bmr,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_fatlevel,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_water,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_guyanliang,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_protein,15);
            ViewUtilsEHC.setCb5Title1Size(cb5_s_bmi,15);
        }


    }

    private void setCb5Title2s(String weight,String fat,String muscle,String bmr,String fatLevel,String water,String guyanliang,String protein,String bmi){
        ViewUtilsEHC.setCb5Title2(cb5_s_weight,weight);
        ViewUtilsEHC.setCb5Title2(cb5_s_fat,fat);
        ViewUtilsEHC.setCb5Title2(cb5_s_muscle,muscle);
        ViewUtilsEHC.setCb5Title2(cb5_s_bmr,bmr);
        ViewUtilsEHC.setCb5Title2(cb5_s_fatlevel,fatLevel);
        ViewUtilsEHC.setCb5Title2(cb5_s_water,water);
        ViewUtilsEHC.setCb5Title2(cb5_s_guyanliang,guyanliang);
        ViewUtilsEHC.setCb5Title2(cb5_s_protein,protein);
        ViewUtilsEHC.setCb5Title2(cb5_s_bmi,bmi);
    }



    private void setCb5Title3(String unit,CombinView5... combinView5s){
        for(CombinView5 cb5:combinView5s){
            if (cb5!=null) cb5.setTitle3(unit);
        }
    }

    private void FV() {
        levelColorView=mRootView.findViewById(R.id.scale_lcv);
        cb5_s_weight=mRootView.findViewById(R.id.cb5_s_weight);
        cb5_s_fat=mRootView.findViewById(R.id.cb5_s_fat);
        cb5_s_bmr=mRootView.findViewById(R.id.cb5_s_bmr);
        cb5_s_fatlevel=mRootView.findViewById(R.id.cb5_s_fatlevel);
        cb5_s_muscle=mRootView.findViewById(R.id.cb5_s_muscle);
        cb5_s_water=mRootView.findViewById(R.id.cb5_s_water);
        cb5_s_guyanliang=mRootView.findViewById(R.id.cb5_s_guyanliang);
        cb5_s_protein=mRootView.findViewById(R.id.cb5_s_protein);
        cb5_s_bmi=mRootView.findViewById(R.id.cb5_s_bmi);
    }

    public void setMeasureResult(ScaleMeasureResult result) {

        //01接收到结果先匹配单位模式
        if (TextUtils.equals(result.getWeightUnit(), ScaleMeasureResultAnalyzer.kg)){
            UserSpUtil.writeBoolean(Constants.IUser.IS_KG_MODEL,true);
            ScaleUser.getUser().setKgModel(true);
        }else {
            UserSpUtil.writeBoolean(Constants.IUser.IS_KG_MODEL,false);
            ScaleUser.getUser().setKgModel(false);
        }
        adapterUnit();

        boolean isKgModel = ScaleUser.getUser().isKgModel();

       UserSpUtil.writeFloat(Constants.IUser.WEIGHT, result.getWeight());
        String disWeight = EHCCommonUtils.unitProcess(result.getWeight(),isKgModel);
        String disMuscle = EHCCommonUtils.unitProcess(result.getMuscleVolume(),isKgModel);
        String disGuYanLiang = EHCCommonUtils.unitProcess(result.getBoneVolume(),isKgModel);
        String disBmi=result.getBmi()+"";


        //02显示相对应的值
        if (ScaleUser.getUser().getAge()<18){
            ToastUtil.show(R.string.select_age_tip);//未满18岁测脂错误提示
            setCb5Title2s(disWeight,N,N,N,N,N,N,N,disBmi);
        }else if (ScaleUser.getUser().getAge()>=18&&result.mesureCode==-1){
            ToastUtil.show(R.string.mesure_fat_error_tip);//未满18岁测脂错误提示
            setCb5Title2s(N,N,N,N,N,N,N,N,N);
            ViewUtils.goneView(levelColorView);
            ViewUtils.setTextViewStr(activityTopTittleTv,getString_(R.string.tizhichen));
            return;
        }else {
            String disFat= NumUtils.numBaoLiuWei1(result.getFat())+"";
            String disBmr=(int)result.getBmr()+"";
            String disInFat=NumUtils.numBaoLiuWei1(result.getVisceralFat())+"";
            String disWater=NumUtils.numBaoLiuWei1(result.getWaterRate())+"";
            String disProtein=NumUtils.numBaoLiuWei1(result.getProtein())+"";
            setCb5Title2s(disWeight,disFat,disMuscle,disBmr,disInFat,disWater,disGuYanLiang,disProtein,disBmi);
        }

        int bmiLevel = ScaleManager.getBMILevel(result.getBmi());

        LogUtils.i(TAG,"---bmiLevel="+bmiLevel);
        
        ViewUtils.displayView(levelColorView);
        if (levelColorView!=null)levelColorView.setLevel(bmiLevel);

        ViewUtils.setTextViewStr(activityTopTittleTv,ScaleManager.getBMIDesc(result.getBmi()));


    }








}
